<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/2/4 16:21
 * Remark:
 */

namespace App\Models\Auth;

use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;

class AuthStaffModel extends BaseModel
{
    protected $table = TableConsts::CY_AUTH_STAFF_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_AUTH_STAFF_TABLE_PRIMARY_KEY;

    protected $fillable = [
        'staff_id',
        'auth_id',
        'auth_key'
    ];

    public $timestamps = false;

    protected static function booted(){}

    public function saveAllAuth($authData){
        $this->insert($authData);
    }

    public function deleteAuthByStaffId($staffId){
        $this->where('staff_id',$staffId)->delete();
    }

    /**
     * 根据用户部门获取权限类型
     * @param $staffId
     * @param $departmentId
     * @param $authType
     * @return mixed
     */
    public function getStaffAuth($staffId,$departmentId): mixed
    {
        return $this
            ->withoutGlobalScopes()
            ->select([
                'staff_auth.auth_id',
                'auth.menu_id',
                'staff_auth.auth_key',
                'auth.auth_code'
            ])
            ->leftJoin(TableConsts::CY_AUTH_AUTH_TABLE_NAME." as auth",'auth.auth_id','staff_auth.auth_id')
            ->where('staff_auth.staff_id',$staffId)
            ->where('staff_auth.department_id',$departmentId)
            ->get();
    }


    /**
     * 根据用户部门获取权限类型
     * @param $staffId
     * @param $departmentId
     * @param $authType
     * @return mixed
     */
    public function getStaffAuthByAuthType($staffId,$departmentId,$authType): mixed
    {
        return $this
        ->withoutGlobalScopes()
        ->select([
            'staff_auth.auth_id',
            'auth.menu_id',
            'staff_auth.auth_key',
            'auth.auth_code'
        ])
        ->leftJoin(TableConsts::CY_AUTH_AUTH_TABLE_NAME." as auth",'auth.auth_id','staff_auth.auth_id')
        ->where('staff_auth.staff_id',$staffId)
        ->where('staff_auth.department_id',$departmentId)
        ->where('auth.auth_type',$authType)
        ->get();
    }
}